/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.model.CurrencyModel;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class CurrenciesAdministrationComponent extends EditTableComponent
{	
	protected CurrencyModel emptyCurrencyModel;
	protected CurrencyModel currencyModel;
	protected List<CurrencyModel> currencyModels;

	protected String defaultCode = "";
	protected String currentCode = "";

	/**
	 * Constructor
	 */
	public CurrenciesAdministrationComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);

		emptyCurrencyModel = new CurrencyModel(-1, "", "", 0.0, false);
		currencyModel = new CurrencyModel(1, "US Dollar", "USD", 1.0000, true);
		currencyModels = new ArrayList<CurrencyModel>();
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		defaultCode = AbstractModel.convertToString(jsonObject, "default_code");
		currentCode = AbstractModel.convertToString(jsonObject, "current_code");
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		currencyModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		CurrencyModel model = new CurrencyModel(jsonObject, defaultCode);
		currencyModels.add(model);
		if (model.getCode().equals(currentCode))
		{
			currencyModel.update(model);
		}
	}

	public CurrencyModel getCurrencyModel(int index) {
		return currencyModels.get(index);
	}

	public CurrencyModel getCurrencyModel() {
		return currencyModel;
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = currencyModels.size();
		if (edit)
			numberOfTableColumns = 10;
		else
			numberOfTableColumns = 9;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Name");
		grid.setText(0, 1, "Code");
		grid.setText(0, 2, "Value");
		grid.setText(0, 3, "Updated");
		grid.setText(0, 4, "Symbol Left");
		grid.setText(0, 5, "Symbol Right");
		grid.setText(0, 6, "Decimal Point");
		grid.setText(0, 7, "Thousands Point");
		grid.setText(0, 8, "Decimal Places");
		if (edit)grid.setText(0, 9, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, currencyModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyCurrencyModel);
	}

	public void displayRow(Grid grid, int row, CurrencyModel model) {
		if (edit) {

			Hidden idHidden = new Hidden();
			idHidden.setName("currencyId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("currencyId-" + row, idHidden);

			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("80px");
			nameTextBox.setText(model.getName());
			elements.put("currencyName-" + row, nameTextBox);
			grid.setWidget(row, 0, nameTextBox);

			TextBox codeTextBox = new TextBox();
			codeTextBox.setWidth("80px");
			codeTextBox.setText(model.getCode());
			elements.put("currencyCode-" + row, codeTextBox);
			grid.setWidget(row, 1, codeTextBox);

			grid.setText(row, 2, String.valueOf(model.getValue()));
			if (model.getDateUpdated() != null) {
				grid.setText(row, 3, DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(model.getDateUpdated()));
			}
			TextBox symbolLeftTextBox = new TextBox();
			symbolLeftTextBox.setWidth("80px");
			symbolLeftTextBox.setText(model.getSymbolLeft());
			elements.put("currencySymbolLeft-" + row, symbolLeftTextBox);
			grid.setWidget(row, 4, symbolLeftTextBox);

			TextBox symbolRightTextBox = new TextBox();
			symbolRightTextBox.setWidth("80px");
			symbolRightTextBox.setText(model.getSymbolRight());
			elements.put("currencySymbolRight-" + row, symbolRightTextBox);
			grid.setWidget(row, 5, symbolRightTextBox);

			TextBox decimalPointTextBox = new TextBox();
			decimalPointTextBox.setWidth("80px");
			decimalPointTextBox.setText(model.getDecimalPoint());
			elements.put("currencyDecimalPoint-" + row, decimalPointTextBox);
			grid.setWidget(row, 6, decimalPointTextBox);

			TextBox thousandsPointTextBox = new TextBox();
			thousandsPointTextBox.setWidth("80px");
			thousandsPointTextBox.setText(model.getThousandsPoint());
			elements.put("currencyThousandsPoint-" + row, thousandsPointTextBox);
			grid.setWidget(row, 7, thousandsPointTextBox);

			TextBox decimalPlacesTextBox = new TextBox();
			decimalPlacesTextBox.setWidth("80px");
			decimalPlacesTextBox.setText(String.valueOf(model.getDecimalPlaces()));
			elements.put("currencyDecimalPlaces-" + row, decimalPlacesTextBox);
			grid.setWidget(row, 8, decimalPlacesTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("currencyDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 9, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getName());
			grid.setText(row, 1, model.getCode());
			grid.setText(row, 2, String.valueOf(model.getValue()));
			if (model.getDateUpdated() != null) {
				grid.setText(row, 3, DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(model.getDateUpdated()));
			}
			grid.setText(row, 4, model.getSymbolLeft());
			grid.setText(row, 5, model.getSymbolRight());
			grid.setText(row, 6, model.getDecimalPoint());
			grid.setText(row, 7, model.getThousandsPoint());
			grid.setText(row, 8, String.valueOf(model.getDecimalPlaces()));
		}
	}
}
